package com.alibaba.cloud.examples.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DiscoverController {

    @NacosInjected
    private NamingService namingService;

    @ResponseBody
    @RequestMapping("discovery")
    public List<Instance> discovery(String serviceName) throws NacosException {
        NamingService namingService = NamingFactory.createNamingService(PropertyKeyConst.SERVER_ADDR);
        List<Instance> allInstances = namingService.getAllInstances(serviceName);
        System.out.println(JSON.toJSONString(allInstances));
        // 发现服务
        return allInstances;
    }
}
